﻿#include "GUIGroupDialog.h"
#include "ui_GUIGroupDialog.h"

#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedData.h"
#include "PickDataProvider/PickedDataProvider.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "OperatorsInterface/TreeEventOperator.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGroupData.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoCommand.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEAAbstractJunction.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractFESubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEFace.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSensors.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"

#include <QMessageBox>

#define _DATAOBJTYPECOLUMN_ 0
#define _DATATYPECOLUMN_ 1
#define _DATAOBJIDCOLUMN_ 2
#define _DATAOBSORTCOLUMN_ 3
#define _DATAOBJTYPE_ 4
#define _DATATYPE_ 5
#define _DATAOBJID_ 6
#define _DATAOBJSORT_ 7

namespace GUI
{
    GUIGroupDialog::GUIGroupDialog(Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/):
        GUIDialogBase(parent), _groupObjID(-1)
    {
        _ui = new Ui::GUIGroupDialog();
        _ui->setupUi(this);
        setWindowTitle(tr("Group"));
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //初始化图片
        this->initImage();
        //初始化数据
        this->init();       
    }
    GUIGroupDialog::GUIGroupDialog(Core::FITKActionOperator* oper, int groupObjID, QWidget* parent /*= nullptr*/) :
        GUIDialogBase(parent), _groupObjID(groupObjID)
    {
        _ui = new Ui::GUIGroupDialog();
        _ui->setupUi(this);
        setWindowTitle(tr("Edit Group"));
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //初始化图片
        this->initImage();
        //初始化数据
        this->init();
        //初始化修改数据
        this->initRevise(groupObjID);
    }
    GUIGroupDialog::~GUIGroupDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }
    void GUIGroupDialog::init()
    {
        Acoustics::FITKAcousticsGroupManager* groupMrg = Acoustics::FITKAcousticsGroupManager::GetGroupManagerFromFrame();
        _ui->lineEdit_Group->setText(groupMrg->checkName(_ui->lineEdit_Group->text()));
        // 初始化TableWidget
        _ui->tableWidget_Element->setColumnCount(1);
        _ui->tableWidget_Element->setHorizontalHeaderLabels(QStringList{ "Element" });
        // 初始化TreeWidget
        _ui->treeWidget_Network->setColumnCount(1);
        _ui->treeWidget_Network->setHeaderLabel("Network");

        // 为Network添加子节点
        QTreeWidgetItem *rootItem = _ui->treeWidget_Network->invisibleRootItem();
        initGeometryTree(rootItem);
        initSystemTree(rootItem);
        initJunctionTree(rootItem);
        initLoadTree(rootItem);
    }
    void GUIGroupDialog::initRevise(int groupObjID)
    {
        // 将现有数据显示在界面上
        Acoustics::FITKAcousticsGroupManager* groupMrg = Acoustics::FITKAcousticsGroupManager::GetGroupManagerFromFrame();
        Acoustics::FITKAcousticsGroupData *groupData = groupMrg->getDataByID(groupObjID);
        // 显示原有组名称
        if (groupData)
            _ui->lineEdit_Group->setText(groupData->getDataObjectName());

        for (int i = 0; i < groupData->getDataCount(); ++i)
        {
            Acoustics::FITKAcousticsGroupData::AcousticsGroupData data = groupData->getGroupDataAt(i);
            // 找到对应的TreeWidget节点
            QTreeWidgetItem *targetItem = findTreeWidgetItem(data._objType, data._type, data._objID);
            if (targetItem)
            {
                // 添加节点到TableWidget
                int row = _ui->tableWidget_Element->rowCount();
                _ui->tableWidget_Element->insertRow(row);
                QTableWidgetItem *itemName = new QTableWidgetItem(targetItem->text(0));
                // 设置左对齐
                itemName->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                _ui->tableWidget_Element->setItem(row, 0, itemName);

                // 自动调整列宽以适应内容
                _ui->tableWidget_Element->resizeColumnsToContents();

                // 保存setData关联的数据
                itemName->setData(_DATAOBJTYPE_, targetItem->data(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_));
                itemName->setData(_DATATYPE_, targetItem->data(_DATATYPECOLUMN_, _DATATYPE_));
                itemName->setData(_DATAOBJID_, targetItem->data(_DATAOBJIDCOLUMN_, _DATAOBJID_));
                itemName->setData(_DATAOBJSORT_, targetItem->data(_DATAOBSORTCOLUMN_, _DATAOBJSORT_));

                // 从TreeWidget中隐藏当前节点
                targetItem->setHidden(true);
            }
        }
    }

    QTreeWidgetItem* GUIGroupDialog::findTreeWidgetItem(Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType objType, int type, int objID)
    {
        QTreeWidgetItem *root = _ui->treeWidget_Network->invisibleRootItem();
        return findTreeWidgetItemRecursive(root, (int)objType, type, objID);
    }

    void GUIGroupDialog::initGeometryTree(QTreeWidgetItem *parent)
    {
        QTreeWidgetItem *geometryItem = new QTreeWidgetItem(parent, QStringList{ "Geometry" });
        geometryItem->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsGeometry);
        //为Geometry添加子节点
        QTreeWidgetItem *nodesItem = new QTreeWidgetItem(geometryItem, QStringList{ "Nodes" });
        QTreeWidgetItem *linesItem = new QTreeWidgetItem(geometryItem, QStringList{ "Lines" });
        QTreeWidgetItem *facesItem = new QTreeWidgetItem(geometryItem, QStringList{ "Faces" });
        QTreeWidgetItem *solidsItem = new QTreeWidgetItem(geometryItem, QStringList{ "Solids" });

        nodesItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Interface::FITKGeoEnum::FITKGeometryComType::FGTPoint);
        linesItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Interface::FITKGeoEnum::FITKGeometryComType::FGTSegment);
        facesItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Interface::FITKGeoEnum::FITKGeometryComType::FGTClosedWireSurface);
        solidsItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Interface::FITKGeoEnum::FITKGeometryComType::FGTClosedSurfaceSolid);
        /*new QTreeWidgetItem(geometryItem, QStringList{ "Vector" });   */

        //调用populateGeometryItems添加数据
        populateGeometryItems(nodesItem, Interface::FITKGeoEnum::FITKGeometryComType::FGTPoint);
        populateGeometryItems(linesItem, Interface::FITKGeoEnum::FITKGeometryComType::FGTSegment);
        populateGeometryItems(facesItem, Interface::FITKGeoEnum::FITKGeometryComType::FGTClosedWireSurface);
        populateGeometryItems(solidsItem, Interface::FITKGeoEnum::FITKGeometryComType::FGTClosedSurfaceSolid);
    }

    void GUIGroupDialog::populateGeometryItems(QTreeWidgetItem *geometryItem, Interface::FITKGeoEnum::FITKGeometryComType type)
    {
        Acoustics::FITKAcousticsGeometryData* geometryData = Acoustics::FITKAcousticsGeometryData::GetGeometryDataFromFrame();
        if (geometryData == nullptr) return;
        QList<Interface::FITKAbsGeoCommand*> geometryList = geometryData->getGeometryByType(type);

        for (int i = 0; i < geometryList.size(); ++i)
        {
            if (geometryList.at(i) == nullptr) continue;
            QTreeWidgetItem *item = new QTreeWidgetItem(geometryItem, QStringList{ geometryList.at(i)->getDataObjectName() });
            // 使用setData关联数据
            item->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsGeometry);
            item->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)type);
            item->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, geometryList.at(i)->getDataObjectID());
            item->setData(_DATAOBSORTCOLUMN_, _DATAOBJSORT_, i);
        }
    }
    void GUIGroupDialog::initSystemTree(QTreeWidgetItem *parent)
    {
        QTreeWidgetItem *SEAsystemItem = new QTreeWidgetItem(parent, QStringList{ "SEAsystem" });
        QTreeWidgetItem *FEsystemItem = new QTreeWidgetItem(parent, QStringList{ "FEsystem" });
        QTreeWidgetItem *sensorItem = new QTreeWidgetItem(parent, QStringList{ "Sensor" });

        SEAsystemItem->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsSEASubsystem);
        FEsystemItem->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsFESubsystem);
        sensorItem->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsSensors);
        //为SEAsystem添加子节点
        QTreeWidgetItem *beamItem = new QTreeWidgetItem(SEAsystemItem, QStringList{ "Beam" });
        QTreeWidgetItem *plateItem = new QTreeWidgetItem(SEAsystemItem, QStringList{ "Plate" });
        QTreeWidgetItem *sigleItem = new QTreeWidgetItem(SEAsystemItem, QStringList{ "Single Curvature Shell" });
        QTreeWidgetItem *doubleItem = new QTreeWidgetItem(SEAsystemItem, QStringList{ "Double Curvature Shell" });
        QTreeWidgetItem *cavityItem = new QTreeWidgetItem(SEAsystemItem, QStringList{ "Cavity" });
        QTreeWidgetItem *semiItem = new QTreeWidgetItem(SEAsystemItem, QStringList{ "Semi-Infine Fluid" });

        beamItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam);
        plateItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate);
        sigleItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell);
        doubleItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell);
        cavityItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity);
        semiItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASemiIInfiniteFluid);
        //调用populateSEAItems添加数据
        populateSEAItems(beamItem, Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEABeam);
        populateSEAItems(plateItem, Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEAFlatPlate);
        populateSEAItems(sigleItem, Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASinglyCurvedShell);
        populateSEAItems(doubleItem, Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEADoublyCurvedShell);
        populateSEAItems(cavityItem, Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity);
        populateSEAItems(semiItem, Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEASemiIInfiniteFluid);
        //为FEsystem添加子节点
        QTreeWidgetItem *faceItem = new QTreeWidgetItem(FEsystemItem, QStringList{ "FE Face" });
        QTreeWidgetItem *structureItem = new QTreeWidgetItem(FEsystemItem, QStringList{ "FE Structure" });
        QTreeWidgetItem *acoustic = new QTreeWidgetItem(FEsystemItem, QStringList{ "FE Acoustic" });

        faceItem->setData(_DATATYPECOLUMN_, _DATATYPE_, 2);
        structureItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractFESubSys::FESubSysType::FEStructure);
        acoustic->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractFESubSys::FESubSysType::FEAcoustic);
        //调用populateFEItems添加数据
        populateFEItems(structureItem, Acoustics::FITKAbstractFESubSys::FESubSysType::FEStructure);
        populateFEItems(acoustic, Acoustics::FITKAbstractFESubSys::FESubSysType::FEAcoustic);
        //为FE Face节点添加数据
        Acoustics::FITKAcousticsDiscModel *FEFaceData = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (FEFaceData == nullptr) return;
        Acoustics::FITKAcousticsFEModel* FEFaceModel = FEFaceData->getFEModel();
        if (FEFaceModel == nullptr) return;
        Acoustics::FITKAcousticsFEFaceManager* FEFaceMgr = FEFaceModel->getFEFaceManager();
        for (int i = 0; i < FEFaceMgr->getDataCount(); ++i)
        {
            QTreeWidgetItem *item = new QTreeWidgetItem(faceItem, QStringList{ FEFaceMgr->getDataByIndex(i)->getDataObjectName() });
            item->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsFESubsystem);
            item->setData(_DATATYPECOLUMN_, _DATATYPE_, 2);
            item->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, FEFaceMgr->getDataByIndex(i)->getDataObjectID());
            item->setData(_DATAOBSORTCOLUMN_, _DATAOBJSORT_, i);
        }
        //为Sensor节点添加数据
        Acoustics::FITKAcousticsDiscModel *sensorData = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (sensorData == nullptr) return;
        Acoustics::FITKAcousticsFEModel* sensorModel = sensorData->getFEModel();
        if (sensorModel == nullptr) return;
        Acoustics::FITKAcousticsSensorsManager* sensorMgr = sensorModel->getSensorManager();
        for (int i = 0; i < sensorMgr->getDataCount(); ++i)
        {
            QTreeWidgetItem *item = new QTreeWidgetItem(sensorItem, QStringList{ sensorMgr->getDataByIndex(i)->getDataObjectName() });
            item->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsSensors);
            item->setData(_DATATYPECOLUMN_, _DATATYPE_, -1);
            item->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, sensorMgr->getDataByIndex(i)->getDataObjectID());
            item->setData(_DATAOBSORTCOLUMN_, _DATAOBJSORT_, i);
        }
    }

    void GUIGroupDialog::populateSEAItems(QTreeWidgetItem *geometryItem, Acoustics::FITKAbstractSEASubSys::SEASubSysType type)
    {
        Acoustics::FITKAcousticsDiscModel *SEAData = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (SEAData == nullptr) return;
        Acoustics::FITKAcousticsSEAModel* SEAModel = SEAData->getSEAModel();
        if (SEAModel == nullptr) return;
        Acoustics::FITKAcousticsSEASubSysManager* subsysMgr = SEAModel->getSEASubSysManager();
        QList< Acoustics::FITKAbstractSEASubSys*> SEAList = subsysMgr->getSubSysByType(type);

        for (int i = 0; i < SEAList.size(); ++i)
        {
            if (SEAList.at(i) == nullptr) continue;
            QTreeWidgetItem *item = new QTreeWidgetItem(geometryItem, QStringList{ SEAList.at(i)->getDataObjectName() });
            // 使用setData关联数据
            item->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsSEASubsystem);
            item->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)type);
            item->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, SEAList.at(i)->getDataObjectID());
            item->setData(_DATAOBSORTCOLUMN_, _DATAOBJSORT_, i);
        }
    }

    void GUIGroupDialog::populateFEItems(QTreeWidgetItem *geometryItem, Acoustics::FITKAbstractFESubSys::FESubSysType type)
    {
        Acoustics::FITKAcousticsDiscModel *FEData = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (FEData == nullptr) return;
        Acoustics::FITKAcousticsFEModel* FEModel = FEData->getFEModel();
        if (FEModel == nullptr) return;
        Acoustics::FITKAcousticsFESubSysManager* subsysMgr = FEModel->getFESubSysManager();
        QList< Acoustics::FITKAbstractFESubSys*> FEList = subsysMgr->getSubSysByType(type);

        for (int i = 0; i < FEList.size(); ++i)
        {
            if (FEList.at(i) == nullptr) continue;
            QTreeWidgetItem *item = new QTreeWidgetItem(geometryItem, QStringList{ FEList.at(i)->getDataObjectName() });
            // 使用setData关联数据
            item->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsFESubsystem);
            item->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)type);
            item->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, FEList.at(i)->getDataObjectID());
            item->setData(_DATAOBSORTCOLUMN_, _DATAOBJSORT_, i);
        }
    }

    void GUIGroupDialog::initJunctionTree(QTreeWidgetItem *parent)
    {
        QTreeWidgetItem *junctionItem = new QTreeWidgetItem(parent, QStringList{ "Junction" });
        junctionItem->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsJunction);
         // 创建Point、Line、Area子节点
        QTreeWidgetItem *pointItem = new QTreeWidgetItem(junctionItem, QStringList{ "Point" });
        QTreeWidgetItem *lineItem = new QTreeWidgetItem(junctionItem, QStringList{ "Line" });
        QTreeWidgetItem *areaItem = new QTreeWidgetItem(junctionItem, QStringList{ "Area" });
        // 创建Point下子节点并添加数据
        QTreeWidgetItem *pSEAItem = new QTreeWidgetItem(pointItem, QStringList{ "SEA" });
        QTreeWidgetItem *pHybridItem = new QTreeWidgetItem(pointItem, QStringList{ "Hybrid" });
        QTreeWidgetItem *pFEItem = new QTreeWidgetItem(pointItem, QStringList{ "FE" });
        populateJunctionItems(pSEAItem, Acoustics::FITKAbstractJunction::JunctionType::SEAPointJunction);
        populateJunctionItems(pHybridItem, Acoustics::FITKAbstractJunction::JunctionType::HybridPointJunction);
        populateJunctionItems(pFEItem, Acoustics::FITKAbstractJunction::JunctionType::FEPointJunction);
        pSEAItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::SEAPointJunction);
        pHybridItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::HybridPointJunction);
        pFEItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::FEPointJunction);
        // 创建Linr下子节点并添加数据
        QTreeWidgetItem *lSEAItem = new QTreeWidgetItem(lineItem, QStringList{ "SEA" });
        QTreeWidgetItem *lHybridItem = new QTreeWidgetItem(lineItem, QStringList{ "Hybrid" });
        QTreeWidgetItem *lFEItem = new QTreeWidgetItem(lineItem, QStringList{ "FE" });
        populateJunctionItems(lSEAItem, Acoustics::FITKAbstractJunction::JunctionType::SEALineJunction);
        populateJunctionItems(lHybridItem, Acoustics::FITKAbstractJunction::JunctionType::HybridLineJunction);
        populateJunctionItems(lFEItem, Acoustics::FITKAbstractJunction::JunctionType::FELineJunction);
        lSEAItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::SEALineJunction);
        lHybridItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::HybridLineJunction);
        lFEItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::FELineJunction);
        // 创建Area下子节点并添加数据
        QTreeWidgetItem *aSEAItem = new QTreeWidgetItem(areaItem, QStringList{ "SEA" });
        QTreeWidgetItem *aHybridItem = new QTreeWidgetItem(areaItem, QStringList{ "Hybrid" });
        QTreeWidgetItem *aFEItem = new QTreeWidgetItem(areaItem, QStringList{ "FE" });
        populateJunctionItems(aSEAItem, Acoustics::FITKAbstractJunction::JunctionType::SEAAreaJunction);
        populateJunctionItems(aHybridItem, Acoustics::FITKAbstractJunction::JunctionType::HybridAreaJunction);
        populateJunctionItems(aFEItem, Acoustics::FITKAbstractJunction::JunctionType::FEAreaJunction);
        aSEAItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::SEAAreaJunction);
        aHybridItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::HybridAreaJunction);
        aFEItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAbstractJunction::JunctionType::FEAreaJunction);
    }
    void GUIGroupDialog::populateJunctionItems(QTreeWidgetItem *geometryItem, Acoustics::FITKAbstractJunction::JunctionType type)
    {
        Acoustics::FITKAcousticsDiscModel *junctionData = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (junctionData == nullptr) return;
        Acoustics::FITKJunctionManager* junctionMgr = junctionData->getJunctionManager();
        QList< Acoustics::FITKAbstractJunction*> junctionList = junctionMgr->getJointsByType(type);
        for (int i = 0; i < junctionList.size(); ++i)
        {
            if (junctionList.at(i) == nullptr) continue;
            QTreeWidgetItem *item = new QTreeWidgetItem(geometryItem, QStringList{ junctionList.at(i)->getDataObjectName() });
            // 使用setData关联数据
            item->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsJunction);
            item->setData(_DATATYPECOLUMN_, _DATATYPE_,(int) type);
            item->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, junctionList.at(i)->getDataObjectID());
            item->setData(_DATAOBSORTCOLUMN_, _DATAOBJSORT_, i);
        }
    }
    void GUIGroupDialog::initLoadTree(QTreeWidgetItem *parent)
    {
        QTreeWidgetItem *loadItem = new QTreeWidgetItem(parent, QStringList{ "Load" });
        loadItem->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsLoad);
        //创建Power、Constraint子节点
        QTreeWidgetItem *powerItem = new QTreeWidgetItem(loadItem, QStringList{ "Power" });
        QTreeWidgetItem *constraintItem = new QTreeWidgetItem(loadItem, QStringList{ "Constraint" });
        //创建Power下子节点并添加数据
        QTreeWidgetItem *pBeam = new QTreeWidgetItem(powerItem, QStringList{ "Beam" });
        QTreeWidgetItem *pShell = new QTreeWidgetItem(powerItem, QStringList{ "Shell" });
        QTreeWidgetItem *pAcoustic = new QTreeWidgetItem(powerItem, QStringList{ "Acoustic" });
        QTreeWidgetItem *pFE = new QTreeWidgetItem(powerItem, QStringList{ "FE Subsystems" });
        populateLoadItems(pBeam, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::PowerSEABeam);
        populateLoadItems(pShell, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::PowerSEAShell);
        populateLoadItems(pAcoustic, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::PowerSEAAcoustic);
        populateLoadItems(pFE, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::PowerFESubsys);
        pBeam->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::PowerSEABeam);
        pShell->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::PowerSEAShell);
        pAcoustic->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::PowerSEAAcoustic);
        pFE->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::PowerFESubsys);
        //创建Constraint下子节点并添加数据
        QTreeWidgetItem *cBeam = new QTreeWidgetItem(constraintItem, QStringList{ "Beam" });
        QTreeWidgetItem *cShell = new QTreeWidgetItem(constraintItem, QStringList{ "Shell" });
        QTreeWidgetItem *cAcoustic = new QTreeWidgetItem(constraintItem, QStringList{ "Acoustic" });
        QTreeWidgetItem *cFE = new QTreeWidgetItem(constraintItem, QStringList{ "FE Subsystems" });
        populateLoadItems(cBeam, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEABeam);
        populateLoadItems(cShell, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEAShell);
        populateLoadItems(cAcoustic, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEAAcoustic);
        populateLoadItems(cFE, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintFESubsys);
        cBeam->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEABeam);
        cShell->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEAShell);
        cAcoustic->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintSEAAcoustic);
        cFE->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ConstraintFESubsys);
        //创建其他子节点并添加数据
        QTreeWidgetItem *ponitForceMoments = new QTreeWidgetItem(loadItem, QStringList{ "Ponit Force & Moments" });
        QTreeWidgetItem *HVACNoiseItem = new QTreeWidgetItem(loadItem, QStringList{ "HVACNoise" });
        QTreeWidgetItem *monopoleItem = new QTreeWidgetItem(loadItem, QStringList{ "Monopole" });
        populateLoadItems(ponitForceMoments, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ExcitationForceMoments);
        populateLoadItems(HVACNoiseItem, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ImportNoiseHVAC);
        populateLoadItems(monopoleItem, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ExcitationMonopole);
        ponitForceMoments->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ExcitationForceMoments);
        HVACNoiseItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ImportNoiseHVAC);
        monopoleItem->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType::ExcitationMonopole);
    }
    void GUIGroupDialog::populateLoadItems(QTreeWidgetItem *geometryItem, Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType type)
    {
        Acoustics::FITKAcousticsPhysics* loadData = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (loadData == nullptr) return;
        Acoustics::FITKAcousticsLoadManager* loadMgr = loadData->getFITKAcousticsLoadManager();
        QList< Acoustics::FITKAcousticsAbstractLoad*> loadList = loadMgr->getLoadByType(type);
        for (int i = 0; i < loadList.size(); ++i)
        {
            if (loadList.at(i) == nullptr) continue;
            QTreeWidgetItem *item = new QTreeWidgetItem(geometryItem, QStringList{ loadList.at(i)->getDataObjectName() });
            // 使用setData关联数据
            item->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, (int)Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType::AcousticsLoad);
            item->setData(_DATATYPECOLUMN_, _DATATYPE_, (int)type);
            item->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, loadList.at(i)->getDataObjectID());
            item->setData(_DATAOBSORTCOLUMN_, _DATAOBJSORT_, i);
        }
    }
    void GUIGroupDialog::on_pushButton_Add_clicked()
    {
        // 获取当前选中的TreeWidget节点
        QTreeWidgetItem *currentItem = _ui->treeWidget_Network->currentItem();
        if (!currentItem) return;
        // 检查节点是否有setData关联的数据
        if (!currentItem->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).isValid()) return;

        // 获取当前选中节点的_DATAOBJID_
        int currentDataObjId = currentItem->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt();

        // 遍历TableWidget中的所有项，检查是否已经存在相同_DATAOBJID_的项
        int rowCount = _ui->tableWidget_Element->rowCount();
        for (int i = 0; i < rowCount; ++i) 
        {
            QTableWidgetItem *item = _ui->tableWidget_Element->item(i, 0);
            if (item && item->data(_DATAOBJID_).toInt() == currentDataObjId) return;
        }
        // 添加节点到TableWidget
        int row = _ui->tableWidget_Element->rowCount();
        _ui->tableWidget_Element->insertRow(row);
        QTableWidgetItem *itemName = new QTableWidgetItem(currentItem->text(0));
        // 设置左对齐
        itemName->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        _ui->tableWidget_Element->setItem(row, 0, itemName);

        // 自动调整列宽以适应内容
        _ui->tableWidget_Element->resizeColumnsToContents();

        // 保存setData关联的数据
        itemName->setData(_DATAOBJTYPE_, currentItem->data(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_));
        itemName->setData(_DATATYPE_, currentItem->data(_DATATYPECOLUMN_, _DATATYPE_));
        itemName->setData(_DATAOBJID_, currentItem->data(_DATAOBJIDCOLUMN_, _DATAOBJID_));
        itemName->setData(_DATAOBJSORT_, currentItem->data(_DATAOBSORTCOLUMN_, _DATAOBJSORT_));

        // 从TreeWidget中隐藏当前节点
        currentItem->setHidden(true);
    }
    
    QTreeWidgetItem* GUIGroupDialog::findTreeWidgetItemRecursive(QTreeWidgetItem *parent, int dataObjType, int dataType, int dataObjId)
    {
        if (parent->data(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_).toInt() == dataObjType &&
            parent->data(_DATATYPECOLUMN_, _DATATYPE_).toInt() == dataType &&
            parent->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt() == dataObjId)
            return parent;
        for (int i = 0; i < parent->childCount(); ++i)
        {
            QTreeWidgetItem *child = parent->child(i);
            QTreeWidgetItem *result = findTreeWidgetItemRecursive(child, dataObjType, dataType, dataObjId);
            if (result) return result;
        }
        return nullptr;
    }

    void GUIGroupDialog::on_pushButton_Remove_clicked()
    {
        // 获取当前选中的 TableWidget 行
        int currentRow = _ui->tableWidget_Element->currentRow();
        if (currentRow < 0) return;

        // 获取 TableWidget 中的项
        QTableWidgetItem *item = _ui->tableWidget_Element->item(currentRow, 0);
        if (!item) return;

        // 获取 setData 关联的数据
        int dataObjType = item->data(_DATAOBJTYPE_).toInt();
        int dataType = item->data(_DATATYPE_).toInt();
        int dataObjId = item->data(_DATAOBJID_).toInt();

        // 查找 TreeWidget 中的对应节点
        QTreeWidgetItem *root = _ui->treeWidget_Network->invisibleRootItem();
        QTreeWidgetItem *targetItem = findTreeWidgetItemRecursive(root, dataObjType, dataType, dataObjId);

        // 显示节点
        if (targetItem)
            targetItem->setHidden(false);
        // 从 TableWidget 中移除当前行
        _ui->tableWidget_Element->removeRow(currentRow);
    }

    void GUIGroupDialog::on_pushButton_OK_clicked()
    {
        Acoustics::FITKAcousticsGroupManager* groupMrg = Acoustics::FITKAcousticsGroupManager::GetGroupManagerFromFrame();
        Acoustics::FITKAcousticsGroupData *groupData;
        //_groupObjID 不为 -1，进行修改操作
        if (_groupObjID != -1)
        {
            groupData = groupMrg->getDataByID(_groupObjID);
            if (!groupData) return;
            if (_ui->lineEdit_Group->text() != groupData->getDataObjectName() && groupMrg->getDataByName(_ui->lineEdit_Group->text()))
            {
                QMessageBox::warning(this, tr("warn"), tr("The name already exists"));
                return;
            }
            groupData->setDataObjectName(_ui->lineEdit_Group->text());
        }
        else if(_groupObjID == -1)
        {
            groupData = new Acoustics::FITKAcousticsGroupData;
            // 新建组时检查名称
            _ui->lineEdit_Group->setText(groupMrg->checkName(_ui->lineEdit_Group->text()));
            groupData->setDataObjectName(_ui->lineEdit_Group->text());
            // 判断是否存在相同名称
            if (groupMrg->getDataByName(_ui->lineEdit_Group->text()))
            {
                QMessageBox::warning(this, tr("warn"), tr("The name already exists"));
                return;
            }
        }
        // 判断名称是否为空
        if (_ui->lineEdit_Group->text().isEmpty())
        {
            QMessageBox::warning(this, tr("warn"), tr("Please set a name"));
            return;
        }
        // 清空原有数据
        groupData->removeGroupData();
        int rowCount = _ui->tableWidget_Element->rowCount();
        for (int i = 0; i < rowCount; ++i)
        {
            QTableWidgetItem *item = _ui->tableWidget_Element->item(i, 0);
            if (item == nullptr) continue;
            Acoustics::FITKAcousticsGroupData::AcousticsGroupData data;
            data._objType = (Acoustics::FITKAcousticsGroupData::AcousticsGroupDataType)(item->data(_DATAOBJTYPE_).toInt());
            data._type = item->data(_DATATYPE_).toInt();
            data._objID = item->data(_DATAOBJID_).toInt();
            groupData->addGroupData(data);
        }
        if (_groupObjID == -1)
            groupMrg->appendDataObj(groupData);
        this->accept();
    }

    void GUIGroupDialog::on_pushButton_Cancel_clicked()
    {
        this->close();
    }

    void GUIGroupDialog::initImage()
    {
        // 修改为图片路径
        QPixmap pixmap("image.png"); 
        _ui->label_Image->setPixmap(pixmap.scaled(_ui->label_Image->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
}
